package com.cleaningcloud.cld.service.impl;

import java.util.List;

import com.cleaningcloud.cld.domain.dto.CldPayConfirmDto;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cleaningcloud.cld.mapper.CldInputDeductionMapper;
import com.cleaningcloud.cld.domain.CldInputDeduction;
import com.cleaningcloud.cld.service.ICldInputDeductionService;

/**
 * 进项抵扣Service业务层处理
 * 
 * @author cleaningcloud
 * @date 2025-04-27
 */
@Service
public class CldInputDeductionServiceImpl implements ICldInputDeductionService 
{
    @Autowired
    private CldInputDeductionMapper cldInputDeductionMapper;

    /**
     * 查询进项抵扣
     * 
     * @param inputDeductionId 进项抵扣主键
     * @return 进项抵扣
     */
    @Override
    public CldInputDeduction selectCldInputDeductionByInputDeductionId(Long inputDeductionId)
    {
        return cldInputDeductionMapper.selectCldInputDeductionByInputDeductionId(inputDeductionId);
    }

    /**
     * 查询进项抵扣列表
     * 
     * @param cldInputDeduction 进项抵扣
     * @return 进项抵扣
     */
    @Override
    public List<CldInputDeduction> selectCldInputDeductionList(CldInputDeduction cldInputDeduction)
    {
        return cldInputDeductionMapper.selectCldInputDeductionList(cldInputDeduction);
    }

    /**
     * 新增进项抵扣
     * 
     * @param cldInputDeduction 进项抵扣
     * @return 结果
     */
    @Override
    public int insertCldInputDeduction(CldInputDeduction cldInputDeduction)
    {
        cldInputDeduction.setCreateTime(DateUtils.getNowDate());
        cldInputDeduction.setCreateId(SecurityUtils.getUserId());
        cldInputDeduction.setCreateName(SecurityUtils.getUsername());
        cldInputDeduction.setFlag(0L);
        cldInputDeduction.setDeductionStatus(0L);
        return cldInputDeductionMapper.insertCldInputDeduction(cldInputDeduction);
    }

    /**
     * 修改进项抵扣
     * 
     * @param cldInputDeduction 进项抵扣
     * @return 结果
     */
    @Override
    public int updateCldInputDeduction(CldInputDeduction cldInputDeduction)
    {
        return cldInputDeductionMapper.updateCldInputDeduction(cldInputDeduction);
    }

    /**
     * 批量删除进项抵扣
     * 
     * @param inputDeductionIds 需要删除的进项抵扣主键
     * @return 结果
     */
    @Override
    public int deleteCldInputDeductionByInputDeductionIds(Long[] inputDeductionIds)
    {
        return cldInputDeductionMapper.deleteCldInputDeductionByInputDeductionIds(inputDeductionIds);
    }

    /**
     * 删除进项抵扣信息
     * 
     * @param inputDeductionId 进项抵扣主键
     * @return 结果
     */
    @Override
    public int deleteCldInputDeductionByInputDeductionId(Long inputDeductionId)
    {
        return cldInputDeductionMapper.deleteCldInputDeductionByInputDeductionId(inputDeductionId);
    }

    /**
     * @Author Shaw
     * @Description 批量修改抵扣
     * @date: 2025-04-27 15:27
     * @param: //TODO
     */
    @Override
    public int batchDeduction(CldInputDeduction cldInputDeduction) {
        int result = 0;
        if (StringUtils.isNull(cldInputDeduction.getInputDeductionIds())) {
            throw new RuntimeException("请选择抵扣记录");
        }
        String[] strs = cldInputDeduction.getInputDeductionIds().split(",");
        if(strs.length != 0){
            for (int i=0;i<strs.length;i++){
                CldInputDeduction obj = cldInputDeductionMapper.selectCldInputDeductionByInputDeductionId(Long.valueOf(strs[i]));
                if(null != obj){
                    obj.setDeductionStatus(1L);
                    obj.setDeductionYear(cldInputDeduction.getDeductionYear());
                    obj.setDeductionMonth(cldInputDeduction.getDeductionMonth());
                    result = cldInputDeductionMapper.updateCldInputDeduction(obj);
                }
            }
        }
        return result;
    }
}
